import { PropertyKey, PropertyDataType } from "@/constants/Enums";
import { editorMap, slotEditorMap } from "./propertyEditors";
import { PropertyKeyToEditor } from "./propertyEditors/config";
import { PropertyRow } from "./types";
import { propertyUtil } from "./PropertyUtil";
import { PropertyItemType } from "@/constants/constants";

/**
 * 属性编辑器的管理器
 * 负责属性编辑器的注册，获取
 */
export class PropertyManager {
  /**
   * 根据一个属性key获得EditType，然后根据EditType获得编辑器组件(propertyEditor)
   * @param key
   * @returns
   */
  getEditorByPropertyKey(key: string) {
    const editor = PropertyKeyToEditor[key];
    return editor;
  }
  /**
   * 通过row获取对应的属性编辑器组件
   * @param row
   * @returns
   */
  getEditorByPropertyRow(row:PropertyRow) {
    const editor = this.getEditorByPropertyKey(row.key);
    // 需要特殊处理渲染的组件 todo
    if (row.specificComponent === PropertyKey.SimulationConfig_UI) {
      return editorMap.ModelsTreeSelectNodeLabel;
    } else if (row.isNumberProperty) {
      return editorMap.RequirementId;
    } else if (row.specificComponent === PropertyKey.NamedElement_name) {
      return editorMap.NamedElement_name;
    } else if (row.specificComponent === "ActualResourcesDeployment") {
      return editorMap.ActualResourcesDeployment;
    } else if (row.key === PropertyKey.Term_TypeOfValueProperty) {
      return editorMap.Model_DropdownSearch;
    }
    if (editor) return editor;
    else {
      return this.getInferredEditor(row);
    }
  }

  // 直接通过editorMap的key获取组件
  getEditorByKey(key: string) {
    return editorMap[key];
  }
  isMultiplicity(mutiple: boolean, upper?: string) {
    if (mutiple) return true;
    if (!upper || upper === "0" || upper === "1") return false;

    if (upper === "*") return true;
    if (+upper > 1) return true;
    return false;
  }

  /**
   * 获得推断得到编辑器组件
   * @param row
   * @returns
   */
  getInferredEditor(row: PropertyRow) {
    // 命名规则
    if (row.key === PropertyKey.Customization_NamingRule_applyToElements) {
      return editorMap.ApplyToElements;
    }

    if (row.key === PropertyKey.Customization_NamingRule_defaultName) {
      return editorMap.DefaultName;
    }

    if (row.key === PropertyKey.Customization_NamingRule_isNameUni) {
      return editorMap.NameUni;
    }

    if (
      row.key === PropertyKey.Customization_NamingRule_prefixRule ||
      row.key === PropertyKey.Customization_NamingRule_separator ||
      row.key === PropertyKey.Customization_NamingRule_prefix
    ) {
      return editorMap.PropertyPrefixSetting;
    }

    switch (row.dataType) {
      case PropertyDataType.STRING:
        if (row.isMultiplicity) {
          return editorMap.InputRows;
        } else {
          return editorMap.Input;
        }
      case PropertyDataType.BOOLEAN:
        if (row.isMultiplicity) {
          return editorMap.CheckboxRows;
        } else {
          return editorMap.Checkbox;
        }
      case PropertyDataType.REAL:
      case PropertyDataType.INTEGER:
      case PropertyDataType.UNLIMITEDNATURAL:
        if (Array.isArray(row.items)) {
          if (row.isMultiplicity) {
            return editorMap.InputRows;
          } else {
            return editorMap.Select;
          }
        }

        if (row.isMultiplicity) {
          return editorMap.InputRows; // 目前不存在这种
        } else {
          return editorMap.InputNumber;
        }
      case PropertyDataType.INSTANCE:
        if (row.itemType === PropertyItemType.items) {
          // 判断是否为枚举
          if (this.isMultiplicity(row.isMultiplicity, row.upper)) {
            return editorMap.Selects; // 复数枚举
          } else {
            return editorMap.Select;
          }
        } else if (row.itemType === PropertyItemType.addItems) {
          // 右侧有个加号下拉按钮
          if (row.isMultiplicity) {
            return editorMap.Models_RightDropdown;
          } else {
            return editorMap.Model_RightDropdown;
          }
        } else {
          if (row.isMultiplicity) {
            return editorMap.Models_TreeSelect;
          } else {
            return editorMap.Model_TreeSelect;
          }
        }

      default:
        break;
    }
  }
  getSlotPropertyEditor(row: Pick<PropertyRow, "dataType" | "items" | "propertyId">) {
    console.log("row =", row);
    if (propertyUtil.isRowDataTypeEnum(row)) {
      return slotEditorMap.SlotSelect;
    }
    if (row.propertyId === PropertyKey.Customization_Banner_text) {
      return slotEditorMap.SlotBanner;
    }
    switch (row.dataType) {
      case PropertyDataType.INSTANCE:
        return slotEditorMap.SlotInstance;
      case PropertyDataType.BOOLEAN:
        return slotEditorMap.SlotCheckbox;
      // test分支还不支持直接修改插槽内的值，仍需通过TextEditor,此处仅为展示，仍可使用slotInput
      // 701已支持，后期合并后，可增加SlotInputNumber
      // case PropertyDataType.REAL:
      // case PropertyDataType.INTEGER:
      // case PropertyDataType.UNLIMITEDNATURAL:
      //   return slotEditorMap.SlotInputNumber;
      default:
        return slotEditorMap.SlotInput;
    }
  }
}

export const propertyManager = new PropertyManager();
